<?php
class Form_Thongtin extends FormProcessor
{
	
	

    
	
	function process($request)
	{
		//var_dump($request);
		global $dbobject;
		$supportobject = new SupportJP();
		
		$this->catalog_id = trim($request['catalog_id']);
		
		$this->thongtin_id= 0;
		
		if (array_key_exists('thongtin_id', $request)) {
				$this->thongtin_id= $request['thongtin_id'];
		}
		
		
		
		
		$this->title = trim($request['title']);
		$this->sabo = trim($request['sabo']);
		$this->content = trim($request['content']);
		
		$this->Date_Month = trim($request['Date_Month']);
		$this->Date_Day = trim($request['Date_Day']);
		$this->Date_Year = trim($request['Date_Year']);
		$this->date = "$this->Date_Year-$this->Date_Month-$this->Date_Day";
		
		$this->active = trim($request['active']);
		
		$this->img = $_FILES['img'];
		
	
	
		
		if (strlen($this->title) <= 0) 
		{
			$this->addError('title_null','Xin mời nhập title!');
		
		}
		
		if (strlen($this->sabo) <= 0)
		{
			$this->addError('sabo_null','Xin mời nhập Sabo!');
		}
		
		if (strlen($this->content) <= 0)
		{
			$this->addError('content_null','Xin mời nhập Content!');
		}
		
		// kiem tra tiep
		
		if ($this->img['name'] != '') 
		{
			$this->img_name_truth = $this->img['name'];
			$this->img_name_alias = date('YmdHis') . '.jpg';
			$supportobject->checkfile_ok($this,$request,'img','upload_image');
		}
		else 
		{
			$this->img_name_truth = '';
			$this->img_name_alias = '';
		}
		
		
		
		if (!$this->hasError()) 
		{	
			if (!$dbobject->query_exist("select id from thongtins where id = $this->thongtin_id"))
            {
			
		
			$sql_add = "
			INSERT INTO `thongtins` 
			( 
			`catolog_id`, `title`, `sabo`, `content`, `datetime`, `img_name_alias`, `img_name_truth`, `active`
			) 
			VALUES ( 
			'$this->catalog_id', '$this->title', '$this->sabo', '$this->content',  concat('$this->Date_Year'  ,'-' , '$this->Date_Month' ,'-' ,'$this->Date_Day')   ,
			'$this->img_name_alias', '$this->img_name_truth','$this->active'
			)";
			
			if ($this->img['name'] != '') 
			{
				move_uploaded_file($this->img['tmp_name'],BASE.'/data/image/upload/thongtin/'.$this->img_name_alias);
			}
			
			mysql_query($sql_add);
			}
			else 
			{
			
			if ($this->img['name'] != '') 
			{
				if ($dbobject->query_cell("select img_name_alias 
				from thongtins where id = $this->thongtin_id",MYSQL_ASSOC,'img_name_alias') != '') 
				{
					unlink(BASE.'/data/image/upload/thongtin/'.$dbobject->query_cell("select img_name_alias 
				from thongtins where id = $this->thongtin_id",MYSQL_ASSOC,'img_name_alias'));
				}
				
				
				move_uploaded_file($this->img['tmp_name'],BASE.'/data/image/upload/thongtin/'.$this->img_name_alias);
			}
			
			$sql_edit ="
			UPDATE `thongtins` SET `catolog_id` = '$this->catalog_id',`title` = '$this->title',`sabo` = '$this->sabo',`content` = '$this->content',
			`datetime` = concat('$this->Date_Year'  ,' ' , '$this->Date_Month' ,' ' ,'$this->Date_Day'),
			`img_name_alias` = '$this->img_name_alias',`img_name_truth` = '$this->img_name_truth',`active` = '$this->active' WHERE `thongtins`.`id` ='$this->thongtin_id' LIMIT 1 ;";
			
			
			mysql_query($sql_edit);
		
			
			}
			//echo 'add csdl';
			//neu add dung lenh insert. con neu nhu edit update, xoa hinh cu, up hinh moi
			
		}
		
		
		return !$this->hasError();
		
	
		
		
	
	}
	
	
}
?>